package flc.ast.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.fragment.HomeFragment;
import flc.ast.fragment.IncomeKind2Fragment;
import flc.ast.databinding.ActivityIncomeBinding;
import flc.ast.fragment.IncomeKind1Fragment;
import flc.ast.fragment.IncomeKind3Fragment;
import stark.common.basic.event.EventStatProxy;

public class IncomeActivity extends BaseAc<ActivityIncomeBinding> {

    private Fragment mCurShowFragment;
    private Fragment incomeKind1Fragment,incomeKind2Fragment,incomeKind3Fragment;

    @Override
    protected int onCreate() {
        return R.layout.activity_income;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivIncomeBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.tvIncomeKind1.setOnClickListener(this);
        mDataBinding.tvIncomeKind2.setOnClickListener(this);
        mDataBinding.tvIncomeKind3.setOnClickListener(this);
        showIncomeKind1Fragment();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvIncomeKind1:
                clearSelection();
                showIncomeKind1Fragment();
                mDataBinding.llIncomeKindTitle.setBackgroundResource(R.drawable.ak4);
                mDataBinding.tvIncomeKind1.setTextColor(Color.WHITE);
                mDataBinding.tvIncomeKind1.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvIncomeKind1.setBackgroundResource(R.drawable.ak5);
                break;
            case R.id.tvIncomeKind2:
                clearSelection();
                showIncomeKind2Fragment();
                mDataBinding.llIncomeKindTitle.setBackgroundResource(R.drawable.ak7);
                mDataBinding.tvIncomeKind2.setTextColor(Color.WHITE);
                mDataBinding.tvIncomeKind2.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvIncomeKind2.setBackgroundResource(R.drawable.ak5);
                break;
            case R.id.tvIncomeKind3:
                clearSelection();
                showIncomeKind3Fragment();
                mDataBinding.llIncomeKindTitle.setBackgroundResource(R.drawable.ak8);
                mDataBinding.tvIncomeKind3.setTextColor(Color.WHITE);
                mDataBinding.tvIncomeKind3.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvIncomeKind3.setBackgroundResource(R.drawable.ak5);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void showIncomeKind1Fragment() {
        if (incomeKind1Fragment == null) {
            incomeKind1Fragment = new IncomeKind1Fragment();
        }
        showFragment(incomeKind1Fragment);
    }

    private void showIncomeKind2Fragment() {
        if (incomeKind2Fragment == null) {
            incomeKind2Fragment = new IncomeKind2Fragment();
        }
        showFragment(incomeKind2Fragment);
    }

    private void showIncomeKind3Fragment() {
        if (incomeKind3Fragment == null) {
            incomeKind3Fragment = new IncomeKind3Fragment();
        }
        showFragment(incomeKind3Fragment);
    }

    private void showFragment(Fragment fragment) {
        if (fragment == null) return;
        if (fragment.isAdded() && fragment.isVisible()) return;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mCurShowFragment != null) {
            transaction.hide(mCurShowFragment);
        }
        if (!fragment.isAdded()) {
            transaction.add(R.id.fl_container, fragment);
        } else {
            transaction.show(fragment);
        }
        mCurShowFragment = fragment;
        transaction.commit();
    }

    private void clearSelection() {
        mDataBinding.tvIncomeKind1.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvIncomeKind1.setTypeface(Typeface.DEFAULT);
        mDataBinding.tvIncomeKind1.setBackground(null);
        mDataBinding.tvIncomeKind2.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvIncomeKind2.setTypeface(Typeface.DEFAULT);
        mDataBinding.tvIncomeKind2.setBackground(null);
        mDataBinding.tvIncomeKind3.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvIncomeKind3.setTypeface(Typeface.DEFAULT);
        mDataBinding.tvIncomeKind3.setBackground(null);
    }

}